class User::InfoManageController < ApplicationController
  before_filter :auth
  layout false
  def accounts
    wh = params[:wh]
    if wh =="main"
      render :partial=>"accounts_main"
    else
        @tempusers=TempUser.paginate :per_page => 15, :page=>params[:page],
           :conditions=>["org_id= ? ",session[CURRENT_USER_ID]],
           :order =>"created_at DESC"
        #  创建账户时的下拉菜单使用
        @all_folders=Folder.find(:all,:conditions=>[" user_id = ? " , session[CURRENT_USER_ID].to_s])
        @all_survey=Survey.find_by_sql("select t1.* from surveys t1, orgs_surveys t2 where t1.id = t2.survey_id and t2.org_id = #{session[CURRENT_USER_ID]} ")
              
    end
  end
   
  def create_account
    begin
      user=User.find(session[CURRENT_USER_ID])
      num=user.temp_users.size
      if num < user.account_num
        temp_user=TempUser.new(params[:temp_user])
        temp_user.password=create_password
        temp_user.name=user.username+"_"+temp_user.name
        temp_user.org_id=(session[CURRENT_USER_ID])
        temp_user.survey_ids = params[:survey_ids].join(',')
        temp_user.save
        flash[:notice]="创建成功！"
        respond_to do |format|
          format.html{ redirect_to(:action=>"accounts")}
        end
      else
        flash[:error1]="子帐户数目达到最大值，不能继续创建！"
        respond_to do |format|
          format.html{ redirect_to(:action=>"accounts")}
        end
      end
    rescue
      flash[:error1]="创建帐号错误"
      respond_to do |format|
        format.html{ redirect_to(:action=>"accounts")}
      end
    end
  end 

  def create_accounts
    survey_ids = params[:survey_ids].join(',')
    begin
      user=User.find(session[CURRENT_USER_ID])
      number=params[:number].to_i
      for i in 1..number
        num=user.temp_users.size
        if num < user.account_num
          temp_user=TempUser.new(params[:temp_user])
          temp_user.name=user.username+"_"+temp_user.name+i.to_s
          temp_user.password=create_password
          temp_user.org_id=(session[CURRENT_USER_ID])
          temp_user.survey_ids = survey_ids
          temp_user.save     
        else
          flash[:error1]="子帐户数目达到最大值，不能继续创建！"
          respond_to do |format|
            format.html{ redirect_to(:action=>"accounts")}
          end
        end
      end
      flash[:notice]="创建成功！"
      respond_to do |format|
        format.html{ redirect_to(:action=>"accounts")}
      end
    rescue
      flash[:error1]="创建失败！"
      respond_to do |format|
        format.html{ redirect_to(:action=>"accounts")}
      end
    end
  end
  
  def create_password
   @str=""
   for i in 1..10
    if i%2==0
       @str += (97+rand(26)).chr.to_s
    else
      @str +=rand(10).to_s
    end
   end
   @str
  end
  
  def create_username
  
  end
  
  def delete
   page=params[:page]
   if page==nil
     page=1
   end
    arrays=params[:array].split(",")
    arrays.each do |a|
       del_tempuser(a)
    end
    flash[:notice]="已经删除！"
    @tempusers=TempUser.paginate :per_page => 15, :page=>page,
             :conditions=>["org_id= ? ",session[CURRENT_USER_ID]],
             :order =>"created_at DESC"
   #redirect_to(:action=>"accounts")
    render :partial=>"temp_user_list"
  end
  
  def del_tempuser(id)
     tempuser=TempUser.find(id)
     tempuser.destroy
  end
  
  def  extend_time
   page=params[:page]
   if page==nil
     page=1
   end
   date_time=params[:time]
   arrays=params[:array].split(",")
    arrays.each do |a|
      tempuser=TempUser.find(a)
      tempuser.last_time=date_time
      tempuser.save
    end
   flash[:notice]="修改完成"
   @tempusers=TempUser.paginate :per_page => 15, :page=>page,
         :conditions=>["org_id= ? ",session[CURRENT_USER_ID]],
         :order =>"created_at DESC"
   #redirect_to(:action=>"accounts")
    render :partial=>"temp_user_list"
  end

  def chpa
    if session[CURRENT_USER_ID] && User.find(session[CURRENT_USER_ID])
    else
        flash[:error_notice]="请先登录再修改密码"
        redirect_to("/login/login")
    end
  end



  def pa_update
    @user=current_user
        if params[:connewpa] == params[:newpa]
           if User.authenticate(@user.username, params[:oldpa])
              @user.password=params[:newpa]
              @user.save!
            flash[:error1]="密码修改成功!"
            redirect_to("/user/info_manage/accounts?wh=main")  
         else
           flash[:error1]="原密码错误！"
           redirect_to("/user/info_manage/accounts?wh=main" )  
         end
      else
        flash[:error2]="两次输入新密码不一致"
        redirect_to("/user/info_manage/accounts?wh=main")  
      end
  end
  
  
  
  def info_update
    begin
      @user=current_user
      @user.name=params[:name]
      @user.email=params[:email]
      @user.tel_phone=params[:tel_phone]
      puts params[:tel_phone]
      puts @user.tel_phone.to_s+"============================================"
      @user.contact=params[:contact]
      @user.save
      flash[:notice]="修改完成"
      redirect_to("/user/info_manage/accounts?wh=main&div=1")  
    rescue
      flash[:notice]="修改时出错"
      redirect_to("/user/info_manage/accounts?wh=main&div=1")  
    end
  end
  
end
